<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/16 0016
 * Time: 17:27
 */
namespace app\admin\controller;
use app\models\Models;

class Table extends Check{
    /**
     * 桌号列表
     * @return \think\response\View
     */
    public function tableList(){
        $get = input('param.');
        $table = new Models('table');
        // 条件查询
        $search = search($get,'b.name','a.create_time','a.status');
        $where  = $search['condition'];
        // 商家登录桌号列表
        $group_arr = self::getGroup();
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $where .= ' and c.bid = '.$group_arr['id'];
            $flag = true;
        }
        // 超管登录商家列表跳转
        if($get['id']){
            $where .= ' and c.bid = '.$get['id'];
            $search['display']['id'] = $get['id'];
        }
        // 预定查询
        if($get['is_reserve'] != ''){
            $where .= ' and a.is_reserve = '.$get['is_reserve'];
            $search['display']['is_reserve'] = $get['is_reserve'];
        }
        $field = "a.*,b.name business,c.bid,c.name type_name,c.people_min,c.people_max,c.lowest_cost";
        $list  = $table->_page($where,$field,'a.id desc',[['table_type c','a.type_id = c.id','left'],['business b','c.bid = b.id','left']],$get['page'],15,$get);
        return view('tableList',['list'=>$list->items(),'page'=>$list->render(),'display'=>$search['display'],'flag'=>$flag]);
    }

    /**
     * 添加桌号
     * @return array|\think\response\View
     */
    public function tableAdd(){
        // 判断商家登录
        $group_arr = self::getGroup();
        if(request()->isAjax()){
            $post = input('param.');
            $table = new Models('table');
            try{
                $data = [
                    'name'    => $post['name'],
                    'type_id' => $post['type_id'],
                    'descrip' => $post['descrip'],
                    'create_time' => time(),
                ];
                $bool = $table->_add($data);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $where = ['a.status'=>Models::STATUS_ON];
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $flag = $group_arr['id'];
            $where['a.bid'] = $group_arr['id'];
        }
        // 桌子类型列表
        $type = new Models('table_type');
        $type_list = $type->joinMany([['business b','a.bid = b.id','left']],$where,'a.*,b.name business');
        return view('tableAdd',['flag'=>$flag,'type'=>$type_list]);
    }

    /**
     * 修改桌号
     * @return array|\think\response\View
     */
    public function tableSave(){
        $table = new Models('table');
        // 判断商家登录
        $group_arr = self::getGroup();
        if(request()->isAjax()){
            $post = input('param.');
            try{
                $data = [
                    'name'    => $post['name'],
                    'type_id' => $post['type_id'],
                    'descrip' => $post['descrip'],
                ];
                $bool = $table->_save($data,['id'=>$post['id']]);
                if(false === $bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $get = input('param.');
        $where = ['a.status'=>Models::STATUS_ON];
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $flag = $group_arr['id'];
            $where['a.bid'] = $group_arr['id'];
        }
        // 获取桌子信息
        $info = $table->_find(['id'=>$get['id']]);
        // 桌子类型列表
        $type = new Models('table_type');
        $type_list = $type->joinMany([['business b','a.bid = b.id','left']],$where,'a.*,b.name business');
        return view('tableSave',['info'=>$info,'type'=>$type_list,'flag'=>$flag]);
    }

    /**
     * 桌号详情
     * @return \think\response\View
     */
    public function tableShow(){
        $get = input('param.');
        $table = new Models('table');
        $join = [
            ['business b','a.bid = b.id','left'],
        ];
        $field = 'a.*,b.name business,b.background,b.descrip b_descrip';
        $info  = $table->joinOne($join,['a.id'=>$get['id']],$field);
        $config = self::getConfig();
        $info['image'] = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'table'. DS .$info['image'];
        $info['background'] = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'background'. DS . 's' .$info['background'];
        return view('tableShow',['info'=>$info]);
    }

    /**
     * 启用
     * @return array
     */
    public function start(){
        if(request()->isAjax()){
            $post = input('param.');
            $admin = new Models('table');
            try{
                $bool = $admin->setValue(['id'=>$post['id']],['status'=>Models::STATUS_ON]);
                if(!$bool){
                    throw new \Exception('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 停用
     * @return array
     */
    public function stop(){
        if(request()->isAjax()){
            $post = input('param.');
            $admin = new Models('table');
            try{
                $bool = $admin->setValue(['id'=>$post['id']],['status'=>Models::STATUS_OFF]);
                if(!$bool){
                    throw new \Exception('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 预定
     * @return array
     */
    public function reserve(){
        if(request()->isAjax()){
            $post = input('param.');
            $admin = new Models('table');
            try{
                $bool = $admin->setValue(['id'=>$post['id']],['is_reserve'=>Models::STATUS_ON]);
                if(!$bool){
                    throw new \Exception('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 取消
     * @return array
     */
    public function cancel(){
        if(request()->isAjax()){
            $post = input('param.');
            $admin = new Models('table');
            try{
                $bool = $admin->setValue(['id'=>$post['id']],['is_reserve'=>Models::STATUS_OFF]);
                if(!$bool){
                    throw new \Exception('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }
}